package com.yyj.service;

import com.yyj.dao.TPropertyAppMapper;
import com.yyj.model.TPropertyApp;
import com.yyj.model.TPropertyAppExample;
import com.yyj.pojo.AccessToken;
import com.yyj.util.WeixinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author leezp
 * @create 2018-04-28 15:49
 **/
@Service
public class AppService {

    @Autowired
    TPropertyAppMapper appMapper;


    public String getAccessToken(){
        TPropertyApp tPropertyApp  = appMapper.selectByPrimaryKey(1L);
        return null != tPropertyApp ? tPropertyApp.getAccessToken() : "";

    }

    public void initAccessToken() throws Exception{
        List<TPropertyApp> appList  = appMapper.selectByExample(null);
        for(TPropertyApp tPropertyApp : appList){
            String appId = tPropertyApp.getAppId();
            String appSecret = tPropertyApp.getAppSecret();
            AccessToken accessToken = WeixinUtil.getAccessToken(appId ,appSecret);
            if(null != accessToken){
                tPropertyApp.setAccessToken(accessToken.getToken());
                appMapper.updateByPrimaryKeySelective(tPropertyApp);
            }
        }

    }
}
